package lib.self.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Build;
import android.os.Build.VERSION;
import android.text.TextUtils;

import java.util.List;

import lib.self.AppEx;
import lib.self.LogMgr;

/**
 * description: 硬件工具类
 * author: Steven Xiao
 * date: 2017/4/25
 */
public class DeviceUtil {
    private static final String TAG = DeviceUtil.class.getSimpleName();


    /**
     * 获取当前手机sdk的版本号
     */
    public static int getSDKVersion() {
        return VERSION.SDK_INT;
    }

    /**
     * 获取手机厂商名字
     */
    public static String getBrand() {
        return Build.BRAND;
    }

    /**
     * 获取手机型号
     */
    public static String getModel() {
        return Build.MODEL;
    }

    /**
     * @return 最大运行内存
     */
    public static long getRuntimeMaxMemory() {
        return Runtime.getRuntime().maxMemory();
    }

    /**
     * 获取当前应用版本名
     */
    public static String getAppVersionName() {
        String versionName = "";
        PackageManager manager = AppEx.getCt().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(AppEx.getCt().getPackageName(), 0);
            versionName = info.versionName;
            if (TextUtils.isEmpty(versionName)) {
                return "";
            }
        } catch (NameNotFoundException e) {
            LogMgr.e(TAG, e.toString());
        }

        return versionName;
    }

    /**
     * 获取当前应用版本号
     */
    public static int getAppVersionCode() {
        int versionCode = -1;
        PackageManager manager = AppEx.getCt().getPackageManager();
        try {
            PackageInfo info = manager.getPackageInfo(AppEx.getCt().getPackageName(), 0);
            versionCode = info.versionCode;
        } catch (NameNotFoundException e) {
            LogMgr.e(TAG, e.toString());
        }
        return versionCode;
    }

    /**
     * @param context 上下文对象
     * @return 获取当前应用程序的包名
     */
    public static String getAppPackageName(Context context) {
        //当前应用pid
        int pid = android.os.Process.myPid();
        //任务管理类
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        //遍历所有应用
        List<RunningAppProcessInfo> infos = manager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (info.pid == pid)//得到当前应用
                return info.processName;//返回包名
        }
        return "";
    }

    /**
     * 方法描述：判断某一应用是否正在运行
     *
     * @param context     上下文
     * @param packageName 应用的包名
     * @return true 表示正在运行，false表示没有运行
     */
    public static boolean isAppRunning(Context context, String packageName) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningTaskInfo> list = am.getRunningTasks(100);
        if (list.size() <= 0) {
            return false;
        }
        for (ActivityManager.RunningTaskInfo info : list) {
            if (info.baseActivity.getPackageName().equals(packageName)) {
                return true;
            }
        }
        return false;
    }

}
